//
//  searchViewController.m
//  Find
//
//  Created by Eason on 14-12-26.
//  Copyright (c) 2014年 Eason. All rights reserved.
//

#import "searchViewController.h"

@interface searchViewController () {
    NSString *_tableName;
    NSMutableArray *_infoMutableArray;
    NSMutableArray *_today;
    NSMutableArray *_yesterday;
    NSMutableArray *_twoDaysAgo;
}

@end

@implementation searchViewController
- (void)viewDidLoad {
    
    
    _tableView.backgroundColor = [UIColor whiteColor];
    //设置系统显示时间的格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    _infoMutableArray = [NSMutableArray array];
    _today = [NSMutableArray array];
    _yesterday = [NSMutableArray array];
    _twoDaysAgo = [NSMutableArray array];
    _tableName    = @"GameScore"; //查询的表名
  
    //创建BmobQuery实例，指定对应要操作的数据表名称
    BmobQuery *query = [BmobQuery queryWithClassName:_tableName];
    //按updatedAt进行降序排列
    [query orderByDescending:@"updatedAt"];
    //返回最多20个结果
    query.limit = 20;
    //执行查询
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        
        //将查询结果初始化News类 并且添加到_infoMutableArray数组中
        for (BmobObject *obj in array) {
            News *info    = [[News alloc] init];
            if ([obj objectForKey:@"cardID"]) {
                info.cardID    = [obj objectForKey:@"cardID"];
            }
            if ([obj objectForKey:@"cardName"]) {
                info.cardName  = [obj objectForKey:@"cardName"];
            }
            if ([obj objectForKey:@"phone"]) {
                info.phoneNum = [obj objectForKey:@"phone"];
            }
            info.time = obj.createdAt;
            [_infoMutableArray addObject:info];
            
            NSString *theTime;//表中条目的时间
            theTime = [formatter stringFromDate:info.time];
            
            NSString *now;
            NSDate *nowTime = [NSDate date];//取得当前时间
            now = [formatter stringFromDate:nowTime];
            
            NSString *yesterday;
            NSDate *yesterdayTime = [NSDate dateWithTimeIntervalSinceNow:-(24*60*60)];//获取昨天时间
            yesterday = [formatter stringFromDate:yesterdayTime];
            
            NSString *twoDayAgo;
            NSDate *twoDayAgoTime = [NSDate dateWithTimeIntervalSinceNow:-(48*60*60)];//获取前天时间
            twoDayAgo = [formatter stringFromDate:twoDayAgoTime];
            
        
//            NSLog(@"%@ AND %@",theTime,now);
        
            if([theTime isEqualToString:now]){
                [_today addObject:info];
            }else if([theTime isEqualToString:yesterday]){
                [_yesterday addObject:info];
            }else if([theTime isEqualToString:twoDayAgo]){
                [_twoDaysAgo addObject:info];
            }
            
            
        }
        NSLog(@"whaha %i",_yesterday.count);
        [_tableView reloadData];
    }];
    
    [super viewDidLoad];

}

#pragma mark 放入tableView节数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

#pragma mark 设置每一节section的cell数目
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0){
        if(!_today.count) //若没有信息则只显示一行
            return 1;
        return _today.count;
    }
    if(section == 1){
        if(!_yesterday.count)
            return 1;
        return _yesterday.count;
    }
    if(section == 2){
        if(!_twoDaysAgo.count)
            return 1;
        return _twoDaysAgo.count;
    }
    else
        return 1;
}


#pragma mark 设置每一节cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"c1";
    UITableViewCell *cell;
    //重用单元格
    cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
#pragma mark 设置第一个section的cell
    if(indexPath.section == 0){
//        NSLog(@"today.count is %i",_today.count);
        if(_today.count == 0){
            cell.textLabel.text = @"没有当天的信息";
            //设置cell行的最后是否有按钮或者小图标
            cell.accessoryType = UITableViewCellAccessoryNone;
            return cell;
        }else{
            NSString *name;
            NSString *cardID;
            NSString *phone;
            News *obj = _today[indexPath.row];
            
            name = obj.cardName;
            cardID = obj.cardID;
            phone = obj.phoneNum;
            
            cell.textLabel.text = obj.cardName;
            cell.detailTextLabel.text = obj.cardID;
            //设置cell行的最后是否有按钮或者小图标
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
#pragma mark 设置第二个section的cell
    if(indexPath.section == 1){
//        NSLog(@"YES.count %d",_yesterday.count);
        if(_yesterday.count == 0){
            cell.textLabel.text = @"没有当天的信息";
            return cell;
        }else{
            NSString *name;
            NSString *cardID;
            NSString *phone;
            News *obj = _yesterday[indexPath.row];
            
            name = obj.cardName;
            cardID = obj.cardID;
            phone = obj.phoneNum;
            
            cell.textLabel.text = obj.cardName;
            cell.detailTextLabel.text = obj.cardID;
            //设置cell行的最后是否有按钮或者小图标
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
        }
    }
#pragma mark 设置第三个section的cell的内容
    if(indexPath.section == 2){
        NSLog(@"two day count %d",_twoDaysAgo.count);
        if(_twoDaysAgo.count == 0){
            cell.textLabel.text = @"没有当天的信息";
            cell.detailTextLabel.text = nil;
            return cell;
        }else{
            NSString *name;
            NSString *cardID;
            NSString *phone;
            News *obj = _twoDaysAgo[indexPath.row];
            
            name = obj.cardName;
            cardID = obj.cardID;
            phone = obj.phoneNum;
            
            cell.textLabel.text = obj.cardName;
            cell.detailTextLabel.text = obj.cardID;
            //设置cell行的最后是否有按钮或者小图标
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    if(indexPath.section == 3){
        cell.textLabel.text = @"查看更多内容";
        cell.detailTextLabel.text = nil;
        cell.accessoryType = UITableViewCellEditingStyleInsert;
    

    }
    return  cell;
}
#pragma mark 设置在点击cell之后弹框效果 由代理监听 （处理alertView）
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    News *obj = nil;
    if(indexPath.section == 0)
        obj = _today[indexPath.row];
    if(indexPath.section == 1)
        obj = _yesterday[indexPath.row];
    if(indexPath.section == 2 && _twoDaysAgo.count)
        obj = _twoDaysAgo[indexPath.row];
    if(obj){
        UIAlertView * alterView = [[UIAlertView alloc]initWithTitle:obj.cardName message:obj.cardID delegate:self cancelButtonTitle:nil otherButtonTitles:@"Enter", nil];
    
    //设置弹框的的Style
    alterView.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alterView textFieldAtIndex:0].text = [@"找回方式:" stringByAppendingString:obj.phoneNum];
        
    [alterView show];//这行没有添加 则无法显示弹框
    }else if(indexPath.section != 3){
        
    UIAlertView *nothing = [[UIAlertView alloc]initWithTitle:@"没有更多消息" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Enter", nil];
    [nothing show];
        
    }
    
    
    
}

#pragma mark 设置表头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    //设置系统显示时间的格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSString *now;
    NSDate *nowTime = [NSDate date];//取得当前时间
    now = [formatter stringFromDate:nowTime];
    
    NSString *yesterday;
    NSDate *yesterdayTime = [NSDate dateWithTimeIntervalSinceNow:-(24*60*60)];//获取昨天时间
    yesterday = [formatter stringFromDate:yesterdayTime];
    
    NSString *twoDayAgo;
    NSDate *twoDayAgoTime = [NSDate dateWithTimeIntervalSinceNow:-(48*60*60)];//获取前天时间
    twoDayAgo = [formatter stringFromDate:twoDayAgoTime];
    
    if(section == 0)
        return now;
    if(section == 1)
        return yesterday;
    if(section == 2)
        return twoDayAgo;
    return @"更多信息请选取下面";
}
-(void)dismissKeyboard:(id)sender{
    [_searchBar resignFirstResponder];
}
@end
